package com.oilsoft.scm.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import org.springframework.core.io.ClassPathResource;

public class JGNUPlot {
	Process p = null;
	BufferedReader stdInput = null;
	BufferedWriter stdOutput = null;
	BufferedReader stdError = null;
		
	/**
	 * Default Constructor
	 */
  public JGNUPlot() {

    try {
    		ClassPathResource pathResource = new ClassPathResource("executable/gnuplot/binary/gnuplot.exe");
    		//File dir =  pathResource.getFile().getParentFile();                                      
			// run gnuplot through a process         
			p = Runtime.getRuntime().exec(pathResource.getFile().getAbsolutePath());  
			
			// Get the Process standart input
			stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
			
			// Get the Process standart output
			stdOutput = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
			
			// Get the Process standart output error
			stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
			
		} catch (IOException e) {
			System.out.println("exception happened - here's what I know: ");
      e.printStackTrace();
      System.exit(-1);
    }
		
  }
	
	/**
	 * Send command to Gnuplot
	 *
	 * @param cmd  Gnuplot shell command.
	 */
  public void cmd(String cmd) {
		
		try {
			stdOutput.write(cmd + "\n");   
			stdOutput.flush();
		} catch (IOException e) {
			System.out.println("exception happened - here's what I know: ");
      e.printStackTrace();
      System.exit(-1);
    } 
	}
	
	/**
	 * Get Gnuplot current outputstream line.
	 */
  public String getLine() {
		
		String aws = null;
		
		try {
			if (stdError.ready()) {
				aws = stdError.readLine();
			}else if(stdInput.ready()){
				aws = stdInput.readLine();
			}
		} catch (IOException e) {
			System.out.println("exception happened - here's what I know: ");
      e.printStackTrace();
      System.exit(-1);
    } 
		
		return aws;
	}
	
	/**
	 * Get next no empty outputstream line.
	 */
  public String getNoEmptyLine() {
	  
		String aws = getLine();
		
		if (aws != null) {
			if (aws.equals("")) {
				aws = getNoEmptyLine();
			}
		}
		return aws;
	}
	
	/**
	 * Get Gnuplot variable.
	 *
	 * @param var  Gnuplot variable name.
	 */
  public String getVar(String var) {
		
		// Ask for the variable
		cmd("if (defined(" + var + ")) print " + var + "; else print \"None\"");
		
		// Get the answer
		String aws = getNoEmptyLine();
		
		// Test if the variable was valid
		if (aws.equals("None")) aws = null;
		
		return aws;
	}

	public Process getP() {
		return p;
	}

	public void setP(Process p) {
		this.p = p;
	}

	public BufferedReader getStdInput() {
		return stdInput;
	}

	public void setStdInput(BufferedReader stdInput) {
		this.stdInput = stdInput;
	}

	public BufferedWriter getStdOutput() {
		return stdOutput;
	}

	public void setStdOutput(BufferedWriter stdOutput) {
		this.stdOutput = stdOutput;
	}

	public BufferedReader getStdError() {
		return stdError;
	}

	public void setStdError(BufferedReader stdError) {
		this.stdError = stdError;
	}
  
	
}
